<!--
 * @Author: HeAo
 * @Date: 2021-10-13 10:54:43
 * @LastEditTime: 2021-10-13 10:58:26
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2105\3-2-Vue\day05\4-全局守卫.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
</head>

<body>
  <div id="app">
    <!-- 4.路由的使用 -->
    <!-- 路由跳转链接 -->
    <router-link to="/user/1001">组件一</router-link>
    <router-link to="/b">组件二</router-link>
    <!-- 路由对应组件加载的窗口 -->
    <router-view></router-view>
  </div>
  <script>
    // 1.声明组件
    let com1 = {
      data() {
        return {
          id: null
        }
      },
      template: `
        <div>
          我是组件一  
          {{id}}
        </div>
      `,
      created() {
        // 动态路由参会会被设置到this.$route.params中
        // console.log(this.$route.params.id);
        this.id = this.$route.params.id
      },
    }
    let com2 = {
      data() {
        return {}
      },
      template: `
        <div>
          我是组件二 
        </div>
      `
    }
    // 2.定义路由
    let router = new VueRouter({
      routes: [
        {
          path: '/user/:id',   // 动态路由，冒号开头的字段是动态参数
          component: com1,// 路径对应加载的组件
          name: 'user'
        },
        { path: '/b', component: com2 },
      ]
    })

    // 5.1全局前置守卫
    router.beforeEach((to, from, next) => {
      console.log('全局前置守卫', to, from);
      next()
    })
    // 5.2全局后置守卫
    router.afterEach((to, from, next) => {
      console.log('全局后置守卫', to, from);
    })

    // 3.注册路由、组件
    let vm = new Vue({
      el: '#app',
      data: {},
      methods: {},
      components: {
        myCom1: com1,
        myCom2: com2
      },
      router
    })
  </script>
</body>

</html>